package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jinpei
 * @version v1.0
 * @date 2019/5/5 19:26
 * @description 查询商品分类的控制层
 **/
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父类目的id查询子类目
     *
     * @param pid：父类目id
     * @return: 商品分类的集合
     */
    @RequestMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        List<CategoryDTO> categoryDTOList = categoryService.queryCategoryByParentId(pid);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据品牌查询商品类目
     * @param brandId 商品id
     * @return 商品分类的集合
     */
    @RequestMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam(value = "id") Long brandId) {
        List<CategoryDTO> categoryDTOList = categoryService.queryCategoryByBrandId(brandId);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据分类id集合查询商品分类
     * @param ids 商品分类id的集合
     * @return 分类的集合
     */
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids) {
        List<CategoryDTO> categoryDTOList = categoryService.queryByIds(ids);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     */
    @GetMapping("levels")
    public ResponseEntity<List<CategoryDTO>> queryAllByCid3(@RequestParam("id") Long id){
        return ResponseEntity.ok(categoryService.queryAllByCid3(id));
    }
}
